<?php
class mycurl {
   /* user agents
   Firefox 3.6.3 Windows: 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.3) Gecko/20100401 Firefox/3.6.3';
   Firefox
   CURL(RedHat): 'curl 7.15.5 (i686-redhat-linux-gnu) libcurl/7.15.5 OpenSSL/0.9.8b zlib/1.2.3 libidn/0.6.5(php)'
   SeaMonkey Linux: 'Mozilla/5.0 (X11; U; Linux i686; de-AT; rv:1.8.0.2) Gecko/20060309 SeaMonkey/1.0'
   */
   private   $ch = null; //The curl handle
   protected $useragent = null;
   protected $url=null;
   protected $followLocation;
   protected $timeout;
   protected $maxRedirects;
   protected $cookieFileLocation = './cookie.txt';
   protected $post;
   protected $postFields;
   protected $referer ="http://www.yahoo.com";

   protected $session;
   protected $webpage;
   protected $includeHeader;
   protected $noBody;
   protected $status;
   protected $curlErrno=0; // store curl errno
   protected $curlError=null; // store curl error
   protected $binaryTransfer;
   public    $authentication = 0;
   public    $auth_name      = '';
   public    $auth_pass      = '';

   /**
    *
    */
   public function __construct($url, $followLocation = true, $timeOut = 30, $maxRedirecs = 4, $binaryTransfer = false, $includeHeader = false, $noBody = false){
       $this->url = $url;
       $this->followLocation = $followLocation;
       $this->timeout = $timeOut;
       $this->maxRedirects = $maxRedirecs;
       $this->noBody = $noBody;
       $this->includeHeader = $includeHeader;
       $this->binaryTransfer = $binaryTransfer;

       $this->cookieFileLocation = dirname(__FILE__).'/cookie.txt';

   }

   public function execCurl($url=null) {
      $this->init($url);
      return $this->exec();
   }

   public function exec() {
      $this->curlError=null;
      if ( $this->ch ) {
         $this->webpage = curl_exec($this->ch);
         $this->status = curl_getinfo($this->ch, CURLINFO_HTTP_CODE);
         if ( ($this->curlErrno=curl_errno($this->ch)) ) {
            $this->curlError = curl_error($this->ch);
         }
         return $this->webpage; 
      }
      return false;
   }

   public function init($url = null)
   {
      if( $url ){
         $this->url = $url;
      }

      $this->ch = curl_init();


      if($this->url) curl_setopt($this->ch, CURLOPT_URL, $this->url);
      //curl_setopt($this->ch, CURLOPT_HTTPHEADER, array('Expect:'));
      curl_setopt($this->ch, CURLOPT_TIMEOUT, $this->timeout);
      curl_setopt($this->ch, CURLOPT_MAXREDIRS, $this->maxRedirects);
      curl_setopt($this->ch, CURLOPT_RETURNTRANSFER,true);
      if ( $this->followLocation ) {
         curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, $this->followLocation);
      }
      curl_setopt($this->ch, CURLOPT_COOKIEJAR, $this->cookieFileLocation);
      curl_setopt($this->ch, CURLOPT_COOKIEFILE, $this->cookieFileLocation);

      if($this->authentication == 1){
         curl_setopt($this->ch, CURLOPT_USERPWD, $this->auth_name.':'.$this->auth_pass);
      }
      if($this->post)
      {
           curl_setopt($this->ch, CURLOPT_POST, true);
           curl_setopt($this->ch, CURLOPT_POSTFIELDS, $this->postFields);
      }

      if($this->includeHeader)
      {
           curl_setopt($this->ch, CURLOPT_HEADER, true);
      }
      if($this->noBody)
      {
          curl_setopt($this->ch, CURLOPT_NOBODY, true);
      }


      curl_setopt($this->ch, CURLOPT_USERAGENT, $this->useragent);

      curl_setopt($this->ch, CURLOPT_REFERER, $this->referer);
   }
   
   public function close() {
      if ( $this->ch ) {
         curl_close($this->ch);
         $this->ch = null;
      }
   }
   /** Auth related */
   public function useAuth($use){
     $this->authentication = 0;
     if($use == true) $this->authentication = 1;
   }

   public function setPass($pass){
     $this->auth_pass = $pass;
   }
   
   public function setName($name){
     $this->auth_name = $name;
   }
   /** Everything else */
   
   public function getCurlError() { return $this->curlError;}
   public function getCurlErrno() { return $this->curlErrno;}
   
   public function getHttpStatus(){
       return $this->status;
   }
   public function reset() {
      $this->curlErrno=0;
      $this->curlError = null;
      $this->status = null;
      $this->close();
      $this->url = null;
   }
   public function setCookieFileLocation($path)
   {
       $this->cookieFileLocation = $path;
   }

   public function setFollowLocation($b) {
      $this->followLocation = $b;
   }
   
   public function setIncludeHeader($b) {
      $this->includeHeader = $b;
   }
   
   public function setPost ($postFields)   {
      $this->post = true;
      $this->postFields = $postFields;
   }

   public function setReferer($referer){
     $this->referer = $referer;
   }
   public function setUrl($url) {
      $this->url = $url;
   }
   public function setUserAgent($userAgent){
       $this->useragent = $userAgent;
   }

   public function __tostring(){
      return $this->webpage;
   }
}
?> 
